fix: replace Windows input busy-poll with WaitForSingleObject#120
Open
tc608 wants to merge 1 commit into
Open
Conversation
…dows The Windows streamData implementation uses PeekConsoleInput + time.Sleep(10ms) in a tight loop, polling at 100Hz even when idle. This causes noticeable CPU usage for all TUI applications on Windows. Replace the busy-poll with WaitForSingleObject on the console input handle, which blocks in the kernel until input is available (zero CPU). A 1s timeout ensures cancellation is detected promptly. Also pre-allocate the peek buffer to avoid repeated 80KB allocations per poll.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Windows
streamDataimplementation interminal_reader_windows.gousesPeekConsoleInput+time.Sleep(10ms)in a tight loop. SincePeekConsoleInputis non-blocking and returns immediately when no input is available, this creates a 100Hz polling loop even when the TUI application is completely idle.This causes noticeable CPU usage for all TUI applications on Windows that use ultraviolet for input handling.
Solution
Replace the busy-poll with
WaitForSingleObjecton the console input handle (conin), which blocks in the kernel until input events are available — zero CPU when idle.A 1-second timeout ensures cancellation is detected promptly.
Additionally, pre-allocate the peek buffer (
peekBuf) and reuse it across iterations viapeekNConsoleInputsInto(), eliminating repeated ~80KB allocations per poll cycle.Changes
time.Sleep(10ms)polling withWaitForSingleObject(conin, 1000)peekNConsoleInputsInto()to reuse pre-allocated buffer"time"importImpact
Testing
Built and tested on Windows 11. Idle CPU dropped from noticeable usage to 0%.